package com.imti.service.impl;

import com.imti.mapper.RepoMapper;
import com.imti.model.Repo;
import com.imti.service.RepoService;
import com.imti.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RepoServiceImpl implements RepoService {
    @Autowired
    private RepoMapper repoMapper;


    @Override
    public PageBean findAllRepo(Repo repo, PageBean page) {
//获取参数
        if(page.getCurrtPage()==null){
            page.setCurrtPage(1);
        }
        if(page.getPageLimit()==null){
            page.setPageLimit(5);
        }
        //查询总条数
        int totalCount=repoMapper.getRepoCount(repo);
        //查询分页列表
        Map map=new HashMap();
        map.put("r",repo);
        map.put("p",page);
        List<Repo> list=repoMapper.findAllRepo(map);
        //封装pagebean
        PageBean pages=new PageBean(totalCount,page.getCurrtPage(),page.getPageLimit(),list,"repocontroller/page",getParams(repo));

        return pages;
    }
    public String getParams(Repo repo){
        StringBuilder sb=new StringBuilder();
        if(repo.getName()!=null && !"".equals(repo.getName())){
            sb.append("&name=").append(repo.getName());
        }
        return sb.toString();
    }


    @Override
    public Integer addRepo(Repo repo) {
        return repoMapper.addRepo(repo);
    }

    @Override
    public Integer updateRepo(Repo repo) {
        return repoMapper.updateRepo(repo);
    }

    @Override
    public Repo getRepoById(Integer id) {
        return repoMapper.getRepoById(id);
    }


}
